﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using CRM.Entity;

using CRM.BLL;
using DP.Web.UI.Controls;
using DP.Web.UI;
using DP.Common;
using System.Data;

namespace CRM.Web.CustomerManage
{
    public partial class List : Common.PageBase
    {

        public string ModifyUrl = "Modify.aspx?CUSTOMERID={0}";
        T_CUSTOMER_BLL bll = new T_CUSTOMER_BLL();
        private T_CUSTOMERTELINDEX_BLL customerTelIndexBll = new T_CUSTOMERTELINDEX_BLL();
        T_DICTIONARY_BLL dBll = new T_DICTIONARY_BLL();
        T_LOCATIONINFORMATION_BLL locationInformationBll = new T_LOCATIONINFORMATION_BLL();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            gvList.PageSize = StringHelper.ToInt(configBll.GetCacheValueByName(this.Page, "PageSize"), 20);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetPopCalender(this.Page, txtBECOMEDATEBegin);
            PageHelper.SetPopCalender(this.Page, txtBECOMEDATEEnd);
            //PageHelper.SetPopCalender(this.Page, txtLASTMODIFYTIMEBegin);
            //PageHelper.SetPopCalender(this.Page, txtLASTMODIFYTIMEEnd);
            //PageHelper.SetPopCalender(this.Page, txtFTIMESTAMPBegin);
            //PageHelper.SetPopCalender(this.Page, txtFTIMESTAMPEnd);
            //PageHelper.SetPopCalender(this.Page, txtBIRTHDATEBegin);
            //PageHelper.SetPopCalender(this.Page, txtBIRTHDATEEnd);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblExportMaxRows.Text = configBll.GetCacheValueByName(this.Page, "ExportMaxRows");
                txtRecordBegin.Text = StaticConstant.RecordBegin;
                txtRecordEnd.Text = StaticConstant.RecordEnd;
                ddlDEALERIDBinding();

                ddlCUSTOMERBASEBinding();
                ddlCUSTOMERTYPEBinding();
                ddlCUSTOMERLEVELBinding();
                ddlSTATUSBinding();
                ddlSPECIALTYPEBinding();



                GetUrlQueryToControl();
                gvDataBinding();
            }
        }

        #region 页面初始化数据绑定




        /// <summary>
        /// DDLs the CUSTOMERBASE binding.
        /// </summary>
        private void ddlCUSTOMERBASEBinding()
        {
            ddlCUSTOMERBASE.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("CustomerBase"));
            ddlCUSTOMERBASE.DataTextField = "DictValue";
            ddlCUSTOMERBASE.DataValueField = "DictKey";
            ddlCUSTOMERBASE.DataBind();
            ddlCUSTOMERBASE.Items.Insert(0, new ListItem("", ""));

        }
        /// <summary>
        /// DDLs the CUSTOMERTYPE binding.
        /// </summary>
        private void ddlCUSTOMERTYPEBinding()
        {
            ddlCUSTOMERTYPE.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("CustomerType"));
            ddlCUSTOMERTYPE.DataTextField = "DictValue";
            ddlCUSTOMERTYPE.DataValueField = "DictKey";
            ddlCUSTOMERTYPE.DataBind();
            ddlCUSTOMERTYPE.Items.Insert(0, new ListItem("", ""));

        }
        /// <summary>
        /// DDLs the CUSTOMERLEVEL binding.
        /// </summary>
        private void ddlCUSTOMERLEVELBinding()
        {
            ddlCUSTOMERLEVEL.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("CustomerLevel"));
            ddlCUSTOMERLEVEL.DataTextField = "DictValue";
            ddlCUSTOMERLEVEL.DataValueField = "DictKey";
            ddlCUSTOMERLEVEL.DataBind();
            ddlCUSTOMERLEVEL.Items.Insert(0, new ListItem("", ""));
        }
        /// <summary>
        /// DDLs the STATUS binding.
        /// </summary>
        private void ddlSTATUSBinding()
        {

            //EnumHelper.FillListControl(ddlSTATUS, typeof(CustomerStatus));

        }
        /// <summary>
        /// DDLs the SPECIALTYPE binding.
        /// </summary>
        private void ddlSPECIALTYPEBinding()
        {
            ddlSPECIALTYPE.Items.Clear();
            EnumHelper.FillListControl(ddlSPECIALTYPE, typeof(CustomerSpecialType));

            ddlSPECIALTYPE.Items.Insert(0, new ListItem("", ""));
        }
        /// <summary>
        ///	经销商编号 下拉列表数据绑定
        /// </summary>
        private void ddlDEALERIDBinding()
        {
            //ddlDEALERID.DataSource = null;
            //ddlDEALERID.DataTextField = "";
            //ddlDEALERID.DataValueField = "";
            //ddlDEALERID.DataBind();
            //ddlDEALERID.Items.Insert(0, new ListItem("", ""));
        }
        #endregion

        /// <summary>
        /// 获取 URL 上的参数存入当页面 控件中。
        /// </summary>
        private void GetUrlQueryToControl()
        {
            PageHelper.GetUrlQueryToControl(this.Page);
        }

        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationSQLString()
        {
            StringBuilder sql = new StringBuilder();
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERID"]))
            {
                sql.Append(" and CUSTOMERID like '%" + Request.QueryString["CUSTOMERID"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERNAME"]))
            {
                sql.Append(" and CUSTOMERNAME like '%" + Request.QueryString["CUSTOMERNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FIRSTCONTACTTEL"]))
            {
                sql.Append(" and FIRSTCONTACTTEL like '%" + Request.QueryString["FIRSTCONTACTTEL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MOBILEPHONE"]))
            {
                sql.Append(" and (MOBILEPHONE = '" + Request.QueryString["MOBILEPHONE"] + "' or HOMEPHONE = '" + Request.QueryString["MOBILEPHONE"] + "' or  FIRSTCONTACTTEL = '" + Request.QueryString["MOBILEPHONE"] + "' ) ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["HOMEPHONE"]))
            {
                sql.Append(" and HOMEPHONE like '%" + Request.QueryString["HOMEPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ADDRESS"]))
            {
                sql.Append(" and ADDRESS like '%" + Request.QueryString["ADDRESS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["COUNTRY"]))
            {
                sql.Append(" and COUNTRY like '%" + Request.QueryString["COUNTRY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PROVINCE"]))
            {
                sql.Append(" and PROVINCE like '%" + Request.QueryString["PROVINCE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PREFECTURECITY"]))
            {
                sql.Append(" and PREFECTURECITY like '%" + Request.QueryString["PREFECTURECITY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CITY"]))
            {
                sql.Append(" and CITY like '%" + Request.QueryString["CITY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VILLAGETOWN"]))
            {
                sql.Append(" and VILLAGETOWN like '%" + Request.QueryString["VILLAGETOWN"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VILLAGE"]))
            {
                sql.Append(" and VILLAGE like '%" + Request.QueryString["VILLAGE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["GROUPTEAM"]))
            {
                sql.Append(" and GROUPTEAM like '%" + Request.QueryString["GROUPTEAM"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERBASE"]))
            {
                sql.Append(" and CUSTOMERBASE like '%" + Request.QueryString["CUSTOMERBASE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERTYPE"]))
            {
                sql.Append(" and CUSTOMERTYPE like '%" + Request.QueryString["CUSTOMERTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERLEVEL"]))
            {
                sql.Append(" and CUSTOMERLEVEL like '%" + Request.QueryString["CUSTOMERLEVEL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SPECIALTYPE"]))
            {
                sql.Append(" and SPECIALTYPE like '%" + Request.QueryString["SPECIALTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BIGCUSTOMERTYPE"]))
            {
                sql.Append(" and BIGCUSTOMERTYPE like '%" + Request.QueryString["BIGCUSTOMERTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATUS"]))
            {
                sql.Append(" and STATUS like '%" + Request.QueryString["STATUS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["IDNUMBER"]))
            {
                sql.Append(" and IDNUMBER like '%" + Request.QueryString["IDNUMBER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["EMAIL"]))
            {
                sql.Append(" and EMAIL like '%" + Request.QueryString["EMAIL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ZIPCODE"]))
            {
                sql.Append(" and ZIPCODE like '%" + Request.QueryString["ZIPCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FAX"]))
            {
                sql.Append(" and FAX like '%" + Request.QueryString["FAX"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("CREATETIME", Request.QueryString["CREATETIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("CREATETIME", Request.QueryString["CREATETIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLEID"]))
            {
                sql.Append(" and CREATEPEOPLEID = '" + Request.QueryString["CREATEPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLE"]))
            {
                sql.Append(" and CREATEPEOPLE like '%" + Request.QueryString["CREATEPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLEID"]))
            {
                sql.Append(" and LASTMODIFYPEOPLEID = '" + Request.QueryString["LASTMODIFYPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLE"]))
            {
                sql.Append(" and LASTMODIFYPEOPLE like '%" + Request.QueryString["LASTMODIFYPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REMARK"]))
            {
                sql.Append(" and REMARK like '%" + Request.QueryString["REMARK"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATUSLOG"]))
            {
                sql.Append(" and STATUSLOG like '%" + Request.QueryString["STATUSLOG"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FTIMESTAMPBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("FTIMESTAMP", Request.QueryString["FTIMESTAMPBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FTIMESTAMPEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("FTIMESTAMP", Request.QueryString["FTIMESTAMPEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["AGE"]))
            {
                sql.Append(" and AGE like '%" + Request.QueryString["AGE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ISACCEPTSMS"]))
            {
                sql.Append(" and ISACCEPTSMS like '%" + Request.QueryString["ISACCEPTSMS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ISACCEPTINFORMATION"]))
            {
                sql.Append(" and ISACCEPTINFORMATION like '%" + Request.QueryString["ISACCEPTINFORMATION"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BIRTHDATEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("BIRTHDATE", Request.QueryString["BIRTHDATEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BIRTHDATEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("BIRTHDATE", Request.QueryString["BIRTHDATEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEALERID"]))
            {
                sql.Append(" and DEALERID = '" + Request.QueryString["DEALERID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEALERNAME"]))
            {
                sql.Append(" and DEALERNAME like '%" + Request.QueryString["DEALERNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEALERCODE"]))
            {
                sql.Append(" and DEALERCODE like '%" + Request.QueryString["DEALERCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ORGNAME"]))
            {
                sql.Append(" and ORGNAME like '%" + Request.QueryString["ORGNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ZONENAME"]))
            {
                sql.Append(" and ZONENAME like '%" + Request.QueryString["ZONENAME"] + "%'");
            }

            if (!String.IsNullOrEmpty(Request.QueryString["BECOMEDATEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("BECOMEDATE", Request.QueryString["BECOMEDATEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BECOMEDATEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("BECOMEDATE", Request.QueryString["BECOMEDATEEnd"].Trim()) + " ");
            }

            if (!UserLoginInfo.IsAdministrator())
            {
                sql.Append(" and BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");

                UserType userType = UserLoginInfo.GetCurrentUserType();
                switch (userType)
                {
                    case UserType.中心部门:
                    case UserType.供应商:
                    case UserType.市场部:
                        {

                        }
                        break;
                    case UserType.服务商:
                        {

                            sql.Append(String.Format("  and customercode in (select vc.customercode from t_vehiclecustomer vc where vc.stationcode = '{0}' union  select customercode from t_customer where 1=1 and CREATEPEOPLEID = '{1}') ", UserLoginInfo.GetCurrentDepartmentCode(), _userLoginInfo.UserEntity.USERID.ToString()));
                        }
                        break;
                    case UserType.经销商:
                        {
                            sql.Append(String.Format("  and customercode in (select vc.customercode from t_vehiclecustomer vc where vc.stationcode = '{0}' union  select customercode from t_customer where 1=1 and CREATEPEOPLEID = '{1}') ", UserLoginInfo.GetCurrentDepartmentCode(), _userLoginInfo.UserEntity.USERID.ToString()));
                        }
                        break;
                }
            }

            //LogHelper.WriteLog("SQL", sql.ToString());
            return sql;

        }

        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding()
        {
            #region 查询条件字符串
            StringBuilder sql = GenerationSQLString();
            #endregion

            List<T_CUSTOMER_Entity> list;

            //获取总的记录条数
            gvList.RecordCount = (int)bll.GetCount(sql.ToString());

            list = bll.GetList(sql.ToString(), gvList.CurrentPageIndex * gvList.PageSize, gvList.PageSize, gvList.SortConditions);

            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "CUSTOMERID" };
            gvList.DataBind();

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            PageHelper.GetControlToUrlQuery(this.Page, ref url);
            Response.Redirect(url);
        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {

            string fileName = "customer";
            fileName += _userLoginInfo.UserEntity.LOGINNAME.ToUpper();
            if(UserLoginInfo.IsAdministrator())
            {
                PageHelper.ShowMessage(this.Page, "请不要用系统管理导出数据！");
                return;
            }
            
            int recordBegin = StringHelper.ToInt(txtRecordBegin.Text.Trim(), 0);
            if (recordBegin != 0)
            {
                recordBegin--;
            }
            int recordEnd = StringHelper.ToInt(txtRecordEnd.Text.Trim(), 0);
            if (recordEnd != 0)
            {
                recordEnd--;
            }
            int recordCount = recordEnd - recordBegin;
            if (recordCount >= StringHelper.ToInt(configBll.GetCacheValueByName(this.Page, "ExportMaxRows"), 10000))
            {
                PageHelper.ShowMessage("导出区间大于单次导出数！");
                return;
            }

            StringBuilder sql = GenerationSQLString();
            DataTable dataTable = bll.GetDataTable(sql.ToString(), recordBegin, recordCount, gvList.SortConditions);


            Dictionary<string, string> dictArea = new Dictionary<string, string>();
            Dictionary<string, string> dictStatus = new Dictionary<string, string>();

            try
            {
                dictArea = locationInformationBll.GetList().ToDictionary(p => p.CODE, p => p.NAME);

            }
            catch (Exception ex)
            {
                
            }
            try
            {
                dictStatus = dBll.GetCacheListByType(this.Page, "CustomerStatus").ToDictionary(p => p.DICTKEY, p => p.DICTVALUE);

            }
            catch (Exception ex)
            {
                
            }

            List<NPOIExcelHelper.FieldInfo> fieldInfos = new List<NPOIExcelHelper.FieldInfo>();

            #region 导出数据列
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CUSTOMERID", "客户编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CUSTOMERCODE", "客户代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CUSTOMERNAME", "客户姓名"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("FIRSTCONTACTTEL", "首选联系号码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("FIRSTCONTACTTYPE", "首选联系方式"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("MOBILEPHONE", "手机号码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("HOMEPHONE", "座机号码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ADDRESS", "详细地址"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("COUNTRY", "国家", dictArea));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("PROVINCE", "省份", dictArea));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("PREFECTURECITY", "地级市", dictArea));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CITY", "区/县/市", dictArea));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("VILLAGETOWN", "乡/镇/街道", dictArea));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("VILLAGE", "村"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("GROUPTEAM", "组/队"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CUSTOMERBASE", "客户群"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CUSTOMERTYPE", "客户类型"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CUSTOMERLEVEL", "客户等级"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("SPECIALTYPE", "特殊类型", EnumHelper.EnumToDictionary(typeof(CustomerSpecialType)).ToDictionary(p => p.Key.ToString(), p => p.Value)));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BIGCUSTOMERTYPE", "大客户类型"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("STATUS", "状态", dictStatus));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("IDNUMBER", "证件号码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("URL", "网站地址"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("EMAIL", "邮件地址"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ZIPCODE", "邮编"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("FAX", "传真"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CREATETIME", "创建时间"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CREATEPEOPLEID", "创建人编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("CREATEPEOPLE", "创建人"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LASTMODIFYPEOPLEID", "最后修改人编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LASTMODIFYPEOPLE", "最后修改人"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("LASTMODIFYTIME", "最后修改时间"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("REMARK", "备注"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("STATUSLOG", "记录状态变更日志"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("FTIMESTAMP", "时间戳"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("AGE", "年龄"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ISACCEPTSMS", "是否接受短信", EnumHelper.EnumToDictionary(typeof(IsAcceptSMS)).ToDictionary(p => p.Key.ToString(), p => p.Value)));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ISACCEPTINFORMATION", "是否接受资料", EnumHelper.EnumToDictionary(typeof(IsAcceptInformation)).ToDictionary(p => p.Key.ToString(), p => p.Value)));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BIRTHDATE", "生日"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("DEALERID", "经销商编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("DEALERNAME", "经销商名称"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("DEALERCODE", "经销商代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ORGNAME", "组织名称"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("ZONENAME", "区域名称"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BUSINESSDIVISIONID", "事业部编号"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BUSINESSDIVISIONNAME", "事业部名称"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BUSINESSDIVISIONCODE", "事业部代码"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("BECOMEDATE", "成为客户日期"));
            fieldInfos.Add(new NPOIExcelHelper.FieldInfo("PURCHASENUMBER", "购买台数"));

            #endregion

            NPOIExcelHelper.MemoryStreamMaxRows = StringHelper.ToInt(configBll.GetCacheValueByName(this.Page, "MemoryStreamMaxRows"), 0);
            fileName = NPOIExcelHelper.DataTableToExcel(dataTable, fieldInfos, fileName + ".xls");
            UserLoginInfo.WriteLog(LogType.导出, "客户资料导出", "客户资料管理", fileName);
            dataTable.Clear();
            dataTable = null;
            GC.Collect();
        }

        #region GridView 事件
        /// <summary>
        /// GridView 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            T_CUSTOMER_Entity entity = new T_CUSTOMER_Entity();
            entity.CUSTOMERID = (sender as GridView).DataKeys[e.RowIndex].Values[0].ToString();

            long rel = bll.Delete(entity);
            customerTelIndexBll.DeleteCustomerTelIndex(entity.CUSTOMERID);
            if (rel > 0)
            {
                Response.Redirect(HttpContext.Current.Request.RawUrl);
            }
            else
            {

            }

        }

        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string temp = string.Empty;
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        GridView gv = (sender as GridView);
                        for (int i = 0; i < gv.Columns.Count; i++)
                        {
                            TemplateField tf = gv.Columns[i] as TemplateField;
                            if (tf != null)
                            {
                                #region 存储值 转换成  显示值

                                switch (tf.HeaderText)
                                {
                                    case "操作":
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                    default:
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;

                                }

                                #endregion
                            }

                            BoundField bf = gv.Columns[i] as BoundField;
                            if (bf != null)
                            {
                                #region 存储值 转换成  显示值

                                switch (bf.DataField)
                                {
                                    case "CUSTOMERBASE":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp = dBll.GetCacheValueByTypeKey(this.Page, "CustomerBase", temp);
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception)
                                            {

                                            }
                                        }
                                        break;
                                    case "CUSTOMERTYPE":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp = dBll.GetCacheValueByTypeKey(this.Page, "CustomerType", temp);
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception)
                                            {

                                                //throw;
                                            }
                                        }
                                        break;
                                    case "CUSTOMERLEVEL":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp = dBll.GetCacheValueByTypeKey(this.Page, "CustomerLevel", temp);
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception)
                                            {

                                                //throw;
                                            }
                                        }
                                        break;
                                    case "STATUS":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp = dBll.GetCacheValueByTypeKey(this.Page, "CustomerStatus", temp);
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception)
                                            {

                                                //throw;
                                            }
                                        }
                                        break;
                                    case "BIGCUSTOMERTYPE":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp = dBll.GetCacheValueByTypeKey(this.Page, "BigCustomerType", temp);
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception)
                                            {

                                                //throw;
                                            }
                                        }
                                        break;
                                    case "SPECIALTYPE":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp =
                                                    ((CustomerSpecialType)
                                                     Enum.Parse(typeof (CustomerSpecialType), temp)).ToString();
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception)
                                            {

                                                //throw;
                                            }
                                        }
                                        break;
                                    case "ISACCEPTSMS":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp = ((IsAcceptSMS) Enum.Parse(typeof (IsAcceptSMS), temp)).ToString();
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception)
                                            {

                                                //throw;
                                            }
                                        }
                                        break;
                                    case "ISACCEPTINFORMATION":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp =
                                                    ((IsAcceptInformation)
                                                     Enum.Parse(typeof (IsAcceptInformation), temp)).ToString();
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception)
                                            {

                                                //throw;
                                            }
                                        }
                                        break;
                                    default:
                                        {

                                        }
                                        break;
                                }

                                #endregion
                            }
                        }
                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }

        /// <summary>
        /// GridView 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }
        #endregion
    }
}
